<?php
   include("connectionParameters.php");
   
         $connection = mysql_connect($host,$user,$pass)
		or die ("We're sorry.. for some reason an error occured when trying to connect to the getub server.<br /> If you keep seeing this error, please contact support@getub.com<br /> Thanks!");

	mysql_select_db($database);
   
// show all books by default
	$query = "select Books.BookId, Books.Title, Books.Author, Books.ISBN, Books.Edition, Books.Course, Books.Professor, Books.Location, Books.Price, Books.PostDate from Books, Hits where Books.BookId=Hits.BookId order by Hits.HitCount desc limit 6;";
	$result = mysql_query($query,$connection)
		or die ("Oops, there was a problem retrieving data. Please try again a bit later. <br /> If you see this error again, please e-mail support@getub.com.<br /> Thanks!");
		while( $row = mysql_fetch_row($result )){
			// get data from database about book 1
			$bookId = $row[0];
			$title = $row[1];
			$author = $row[2];
			$isbn = $row[3];
			$edition = $row[4];
			$course = $row[5];
			$professor = $row[6];
			$location = $row[7];
			$price = $row[8];
			$postDate = $row[9];
			
			// extract data in YYYY-MM-DD format
			$dateData = explode("-",$postDate);
			$year = $dateData[0];
			$month = $dateData[1];
			$day = $dateData[2];
			
			// converting to format "Wednesday, August 05, 2009"
			$labelData = date("l,F d,Y", mktime(0,0,0,$month, $day, $year));
			$bookLink = $title.",".$author.",".$edition." - ".$price."(".$location.")";
			
			$TitleCharacterLimit = 30;
			$AuthorCharacterLimit = 35;
			
			// takes care of title being too long
			if (strlen($title) > $TitleCharacterLimit){
				$title = substr($title, 0, $TitleCharacterLimit)."...";
			}
			if (strlen($author) > $AuthorCharacterLimit){
				$author = substr($author, 0, $AuthorCharacterLimit)."...";
			}
			
			//echo "<li><a href='bookDetails.php?bookId='".$bookId.">".$bookLink."</a> </li>";
			
			// DONT FORGET TO INCLUDE BOOK ID BELOW IN A HREF LATER!!!!!!
			echo "<ul class='popularul'><li><span class='popularimg'></span>
				 <span class='rbookhd2'><a href='bookDetails.php?bookId=$bookId'>".$title."</a></span><br />
				 <span class='rauthor2'>".$author."</span></li>";
			
			
			// get data from database about book 2	 
			$row2 = mysql_fetch_row($result);
			
			$bookId2 = $row2[0];
			$title2 = $row2[1];
			$author2 = $row2[2];
			$isbn2 = $row2[3];
			$edition2 = $row[4];
			$course2 = $row[5];
			$professor2 = $row[6];
			$location2 = $row[7];
			$price2 = $row[8];
			$postDate2 = $row[9];
			
			// takes care of title being too long
			if (strlen($title2) > $TitleCharacterLimit){
				$title2 = substr($title2, 0, $TitleCharacterLimit)."...";
			}
			if (strlen($author2) > $AuthorCharacterLimit){
				$author2 = substr($author2, 0, $AuthorCharacterLimit)."...";
			}
		
			echo "<li class='btmli'><span class='popularimg'></span><span class='rbookhd2'><a href='bookDetails.php?bookId=$bookId2'>".$title2."</a></span><br /><span class='rauthor2'>".$author2."</span></li></ul>";
			 }
			
			mysql_close($connection);
			?>